import QtQuick 2.8
import QtQuick.Layouts 1.2
import QtQuick.Controls 2.14

//QML BOOK中文版：https://www.bookstack.cn/read/QmlBook-In-Chinese/quick_starter-qmlqml_syntax.md
Page { //https://doc.qt.io/qt-5/qml-qtquick-controls2-page.html
    id: pageTaskD //任何在QML文档中的元素都可以使用它们的id进行访问
    title: qsTr("选择考场")
    visible: true
 
    // property StackView stackTest: null
    property StackView stack: null
    // property int index
    property alias text: inputD.text //获取输入框的文本 // alias 关键字声明属性的别名
 
    Keys.enabled: true
    // focus: true //need focus to receive key events
    Keys.onPressed: {
        // 按键名称：https://doc.qt.io/qt-5/qt.html#Key-enum
        if(event.key == Qt.Key_Escape){
            console.log("Esacpe被按下")
            // stackTest.custPop() // 函数未调用到
            stack.custPop()
        }
        if(event.key == Qt.Key_F1){ // 进入计数页面
            console.log("输入的内容为： " + text) //控制台输出信息
            console.log("Enter被按下")
            judgeIndex()
        }
        event.accepted = true
    }

    //页头
    header: ToolBar {
        background: Rectangle {
            implicitWidth: 100
            implicitHeight: 40
            opacity: 1
            color: "#3f51b5"
        }
        RowLayout {
            anchors.fill: parent //元素可以嵌套，这意味着一个父元素可以拥有多个子元素。子元素可以通过访问parent关键字来访问它们的父元素
            ToolButton {
                text: qsTr("‹返回[Esc]")
                onClicked: stackTest.custPop()
            }
            Label {
                text: "请选择场次"
                font.pointSize: 16
                color: '#ffffff'
                elide: Label.ElideRight
                horizontalAlignment: Qt.AlignHCenter
                verticalAlignment: Qt.AlignVCenter
                Layout.fillWidth: true
            }
        }
    }
    //主体
    Rectangle{
        width: parent.width;
        height: parent.height;
        color: "linen"
        Text{
            id: textD
            text:"请输入考场号(共"+ rootWindow.batchCount +"考场): "
            x:parent.width/10;
            y:parent.height/4;
            width: parent.width/2;
            height: parent.height/8;
            font.pointSize: 36 //https://blog.csdn.net/bzs2510568513/article/details/120056541
            font.bold: true
        }
        TextInput{ //https://www.bookstack.cn/read/QmlBook-In-Chinese/quick_starter-input_element.md
            id: inputD
            x:parent.width/2;
            y:parent.height/2;
            width: parent.width/2;
            height: parent.height/8;
            focus: true
            font.pointSize:42
            font.bold: true
        }
    }
    //页尾
    footer: Rectangle{
        width: parent.width
        height: 40
        color: '#3f51b5'
        
        Text {
            anchors.centerIn: parent
            font.pointSize: 16
            color: '#ffffff'
            text: "考场号[0~9] 确认[Enter]"
        }
    }

    // 判断输入的考场号是否在范围内，是则传值，否则提示
    function judgeIndex() {
        if(parseInt(text) > 0){
            if(parseInt(text) <= rootWindow.batchCount){
            // index = Number(pageTaskD.text) //https://doc.qt.io/qt-5/qml-qtqml-number.html#fromLocaleString-method
            rootWindow.batchIndex = parseInt(text) //https://www.cnblogs.com/linuxandmcu/p/11594541.html
            console.log("输入的考场号为： " + rootWindow.batchIndex)

            // stackTest.custPush('PageTaskCounter.qml', {})
            stack.custPush('PageTaskCounter.qml')
            }
        }else{
            rootWindow.popupTips('输入有误') //TODO 提示未成功
        }
    }

    Component.onCompleted: { //组件创建时自动调用
        // print("PageTaskC.onCompleted() called")  
    }
    Component.onDestruction:{//组件消除的时候自动调用
    }
}